<?php

class registration extends a_front_controller {

    public $form_data;
    public $form_errors;

    public function __construct() {
        parent::__construct(true);
        $this->form_data = array();
        $this->get_groups_n_fields();
    }

    public function batch_assign(){
        $this->assign("form_data", $this->form_data);
        $this->assign("form_errors", $this->form_errors);
    }

    public function index() {

    }
    
    public function create_user(){
        if($this->form->valid){

            $vars = $this->form->get_post();
            
            if($this->verify_status_number($vars['status_number']))
            {
                $new_user = user_service_l_user::create_user($vars);

                //add billing addresses
                user_service_l_user::add_user_billing_address($vars, $new_user->id);

                //add shipping address
                user_service_l_user::add_user_shipping_address($vars, $new_user->id);
            }
        }else{
            $this->form_errors = $this->form->errors;
        }
    }

    private function verify_status_number($statusNumber)
    {
        if(is_numeric($statusNumber))
        {
            //check if this is 9 digits
            if(strlen($statusNumber)==9)
            {
                  if($statusNumber{0}==$statusNumber{1} && $statusNumber{0}==$statusNumber{2})
                  {
                        show_error("Not a valid number<br>");
                  }
                  else
                  {
                        return TRUE;
                  }
            }
            else
            {
                show_error("Status Number Not 9 Digits");
            }
        }
        else
        {
            show_error("Not a number");
        }

        return FALSE;
    }

    public function user_check(){
        return false;
    }
    protected function get_groups_n_fields() {
        $obj = new field_groups_m_user();
        $groups = $obj->where("visible","1")->get_iterated();

        $this->form->open(base_url()."index.php/user/registration", "commentForm", array('class' => "cmxform"));
        foreach ($groups as $group) {
            $this->form->fieldset($group->group_description);
            $fields = $group->reg_fields_m_user->order_by("order")->get();
            foreach ($fields as $field) {
                switch ($field->rendering_class) {

                    case "text":
                        $this->form->text($field->field_name, $field->field_description,
                                $field->validation_rule,
                                $field->default_value,
                                array('title' => $field->field_note,
                                      'class' => strcmp($field->field_name, "email")== 0 ? "required email" : "required",
                                      'minlength' => "2"));
                        break;
                    case "textarea":
                        $this->form->textarea($field->field_name, $field->field_description, $field->validation_rule, $field->default_value, array('title' => $field->field_note));
                        break;
                    case "radiogroup":
                        $radios = array();
                        $pieces = explode(";", $field->values);
                        foreach ($pieces as $piece)
                            $radios[] = explode("=", $piece);
                        $this->form->radiogroup($field->field_name, $radios, $field->field_description, $field->default_value, $field->validation_rule, array('title' => $field->field_note));
                        break;
                    case "checkbox":
                        $this->form->br();
                        $this->form->checkbox($field->field_name, $field->values, $field->field_description,$field->default_value=="checked" ? TRUE: FALSE, $field->validation_rule, array('title' => $field->field_note));
                        break;
                    case "checkgroup":
                        $options = array();
                        $pieces = explode(";", $field->values);
                        foreach ($pieces as $piece)
                            $options[] = explode("=", $piece);
                        $this->form->checkgroup($field->field_name, $options, $field->field_description, $field->default_value, $field->validation_rule, array('title' => $field->field_note));
                        break;
                    case "select_one"    :
                        $options = array();
                        $pieces = explode(";", $field->values);
                        foreach ($pieces as $piece){
                            $even_more_pieces = explode("=", $piece);                        
                            $options[$even_more_pieces[0]] = $even_more_pieces[1];
                        }
                        $this->form->select($field->field_name, $options,  $field->field_description, $field->default_value,$field->validation_rule, array('title' => $field->field_note));
                        break;
                    default:
                        break;
                }
                if(strlen($field->field_error_msg)>0){
                    $this->form->set_error($field->field_name, $field->field_error_msg);
                }
            }
        }
        $this->form->indent(150);
        $this->form->hidden(i_handle_posts::OP_CODE,"create_user");
        $this->form->submit();
        $this->form_data[] = $this->form->get();
    }

}
?>